package com.example.easybuy.service;

import com.example.easybuy.dao.ProductsManageDao;
import com.example.easybuy.pojo.Page;
import com.example.easybuy.pojo.Products;
import com.example.easybuy.pojo.ProductsManage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductsManageServiceImpl implements ProductsManageService{

    private Logger logger=Logger.getLogger(getClass());

    @Autowired
    private ProductsManageDao productsManageDao;

    /*
   查询全部商品 分页
    */
    @Override
    public List<ProductsManage> getProductsManageAll(Page page) {
        logger.debug("ProductsManageServiceImpl-getProductsManageAll()...start...page："+page);
        //查询数据总条数
        int totalDataCount=productsManageDao.getProductsCountAll();
        //页面容量
        page.setPageSize(5);
        //总数居条数
        page.setTotalDataCount(totalDataCount);
        //从第几条开始显示=（当前页码-1）*页面容量
        int pageNo=(page.getCurrentPageNo()-1)*page.getPageSize();
        page.setCurrentPageNo(pageNo);

        List<ProductsManage> productsManageList=productsManageDao.getProductsManageAll(page);
        logger.debug("ProductsManageServiceImpl-getProductsManageAll()-productsManageList："+productsManageList);
        return productsManageList;
    }

    /*
    根据id删除商品信息
     */
    @Override
    public String deleteProductsById(String id) {
        logger.debug("ProductsManageServiceImpl-deleteProductsById()-id："+id);
        int proId=Integer.valueOf(id);
        int deleteRow=productsManageDao.deleteProductsById(proId);
        String success=null;
        if (deleteRow>0){
            success="删除成功";
        }else {
            success="删除失败";
        }
        return success;
    }

    /*
    添加商品信息
     */
    @Override
    public String addProducts(ProductsManage productsManage) {
        logger.debug("ProductsManageServiceImpl-addProducts()-productsManage："+productsManage);
        int addRow=productsManageDao.addProducts(productsManage);
        String success=null;
        if (addRow>0){
            success="添加成功";
        }else {
            success="添加失败";
        }
        logger.debug("ProductsManageServiceImpl-addProducts()-success："+success);
        return success;
    }

    /*
    根据id查询商品信息
     */
    @Override
    public ProductsManage getProductsById(String id) {
        logger.debug("ProductsManageServiceImpl-getProductsById()-id："+id);
        int proId=Integer.valueOf(id);
        ProductsManage productsManage=productsManageDao.getProductsById(proId);
        logger.debug("ProductsManageServiceImpl-getProductsById()-productsManage："+productsManage);
        return productsManage;
    }

    /*
   修改商品
    */
    @Override
    public String updateProducts(ProductsManage productsManage) {
        logger.debug("ProductsManageServiceImpl-updateProducts()-productsManage："+productsManage);
        int updataRow=productsManageDao.updateProducts(productsManage);
        String success=null;
        if (updataRow>0){
            success="修改成功";
        }else {
            success="修改失败";
        }
        logger.debug("ProductsManageServiceImpl-updateProducts()-success："+success);
        return success;
    }

    /*
    查询最新添加的商品
     */
    @Override
    public ProductsManage getProductsNew() {
        logger.debug("ProductsManageServiceImpl-getProductsNew()-...start...");
        ProductsManage productsManage=productsManageDao.getProductsNew();
        logger.debug("ProductsManageServiceImpl-getProductsNew()-productsManage："+productsManage);
        return productsManage;
    }


}
